<?php
/**
 * 聊天
 * User: Tsang
 * Date: 2017/12/21 10:20
 *
 */

namespace app\api\controller;


use think\Cache;
use think\Loader;
use app\api\service\AesEncrypt;

class Chat extends Base
{

    //检查权限作用域
    protected $beforeActionList  = [
        'checkPrimaryScope' => [
            'only' => 'mkit,chatList,readChat,setchatcache'
        ],
    ];

    /**
     * 聊天界面
     */
    public function mkit(){
        $m_chat = db('chat');
        $chat = $m_chat->where(['typeid' => $this->user['uid'],'type' => '1'])->find();
        if(!$chat) $this->error('找不到聊天账号');
        $this->assign('uid',$chat['userid']);
        $this->assign('pwd',$chat['pwd']);
        $this->assign('appkey',config('openim.appkey'));
        if(!array_key_exists('touid',$this->requestData)) $this->error('对方ID不能为空');
        $toUserid = intval($this->requestData['touid']);
        $type = intval($this->requestData['type']);
        switch($type){
            case 1:
                $touser = $m_chat->where(['typeid' => $toUserid,'type' => $type])->find();
                if(!$touser) $this->error('找不到对方账号');
                break;
            case 2:
                $touser = $m_chat->where(['id' => $toUserid])->find();
                if(!$touser) $this->error('找不到对方账号');
                break;
        }

        $this->assign('touid',$touser['userid']);
        $data = [
            'token'=>$this->requestData['access_token'],
            'timestamp'=>time(),
            'uid' => $chat['id'],
            'goods_id' => array_key_exists('goods_id',$this->requestData) ? $this->requestData['goods_id'] : 0,
            'to_uid' => $touser['id'],
        ];
        $endata = (new AesEncrypt())->encrypt(json_encode($data));
        $this->assign('token',$endata);

        return $this->fetch();
    }

    /**
     * 设置聊天缓存
     */
    public function setchatcache(){
        $to_uid=!empty($this->requestData['to_uid'])?$this->requestData['to_uid']:0;
        $goods_id=!empty($this->requestData['goods_id'])?$this->requestData['goods_id']:0;

        $goods_result = db('goods')->where(array('id'=>$goods_id))->find();

        if(empty($goods_result))return $this->ajaxReturn(['message' => '商品不存在']);

        $result = (new \app\api\logic\Chat())->setChatCache($this->user['userId'],$to_uid,$goods_id);
        return $this->ajaxReturn(
            $result
        );
    }

    /**
     * 获取聊天列表
     */
    public function chatList(){
        $chat = db('chat')->where(['typeid' => $this->user['uid'],'type' => 1])->find();
        if(!$chat) return array('message' => '找不到聊天账号');
        return $this->ajaxReturn(
            (new \app\api\logic\Chat())->chatList($chat['id'])
        );
    }

    /**
     * 阅读信息
     */
    public function readChat(){
        $chat = db('chat')->where(['typeid' => $this->user['uid'],'type' => 1])->find();
        if(!$chat) return array('message' => '找不到聊天账号');
        if(!array_key_exists('touid',$this->requestData)) return $this->ajaxReturn(['message' => '对方聊天账号不能为空']);
        return $this->ajaxReturn(
            (new \app\api\logic\Chat())->readChat($chat['id'],intval($this->requestData['touid']))
        );
    }

    /**
    *客服列表
    */
    public function customer_service(){
        $chat = db('chat')->where(['type' => 2])->field('nickname,userid as baichuan_user')->select();

        return $this->ajaxReturn(
            $chat
        );
    }


}